package com.cx.bank.view;

import com.cx.bank.util.InvalidDepositException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 20:07
 */
public class WithDrawalsMoneyView extends JFrame {
    private JButton confirm;
    private JLabel moneyLabel;
    public JTextField moneyText;
    private Double money;


    public WithDrawalsMoneyView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);
        moneyLabel = new JLabel("取款金额");
        confirm = new JButton("确认");
        moneyText = new JTextField(20);

        confirm.setBounds(250, 500, 120, 25);
        moneyLabel.setBounds(150, 250, 120, 25);
        moneyText.setBounds(250, 250, 200, 30);

        add(confirm);
        add(moneyLabel);
        add(moneyText);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners() {
        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                money = Double.valueOf(moneyText.getText());
                try {
                    IndexView.getManager().withdrawals(money);
                } catch (InvalidDepositException ex) {
                    ex.printStackTrace();
                }
                //关闭当前页面
                WithDrawalsMoneyView.this.dispose();

            }
        });
    }

    public static void main(String[] args) {
        WithDrawalsMoneyView withDrawalsMoneyView = new WithDrawalsMoneyView();
        withDrawalsMoneyView.show();
    }
}
